package cn.com.ebidding.controller;

import cn.com.ebidding.business.annotation.BussinessLog;
import cn.com.ebidding.business.entity.Group;
import cn.com.ebidding.business.entity.Role;
import cn.com.ebidding.business.enums.ResponseStatus;
import cn.com.ebidding.business.enums.UserStatusEnum;
import cn.com.ebidding.business.service.SysGroupService;
import cn.com.ebidding.business.vo.GroupConditionVO;
import cn.com.ebidding.framework.object.PageResult;
import cn.com.ebidding.framework.object.ResponseVO;
import cn.com.ebidding.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户管理
 *
 * @author 扫地僧 (saods@ebidding.com.cn)
 * @version 1.0
 * @website https://ksp.ebidding.com.cn
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@RestController
@RequestMapping("/groups")
public class RestGroupController {
    @Autowired
    private SysGroupService groupService;

    @RequiresPermissions("groups")
    @PostMapping("/list")
    public PageResult list(GroupConditionVO vo) {
        PageInfo<Group> pageInfo = groupService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }


    @RequiresPermissions("group:add")
    @PostMapping(value = "/add")
    @BussinessLog("添加用户")
    public ResponseVO add(Group group) {
        Group u = groupService.getByGroupName(group.getName());
        if (u != null) {
            return ResultUtil.error("该用户组名["+group.getName()+"]已存在！请更改用户名");
        }
        try {
            groupService.insert(group);
            return ResultUtil.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("error");
        }
    }


    @RequiresPermissions("group:get")
    @PostMapping("/groupsListWithSelected")
    public ResponseVO<List<Group>> rolesWithSelected(Long articleId,Integer authType) {
        return ResultUtil.success(null, groupService.queryGroupListWithSelected(articleId, authType));
    }


    @RequiresPermissions("group:get")
    @PostMapping("/get/{id}")
    @BussinessLog("获取用户组详情")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.groupService.getByPrimaryKey(id));
    }

    @RequiresPermissions("group:edit")
    @PostMapping("/edit")
    @BussinessLog("编辑用户组")
    public ResponseVO edit(Group group) {
        try {
            groupService.updateSelective(group);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("用户修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }


    @RequiresPermissions( "group:delete")
    @PostMapping(value = "/remove")
    @BussinessLog("删除用户")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        Group group = new Group();
        group.setStatus(UserStatusEnum.DISABLE.getCode());
        for (Long id : ids) {
            group.setId(id);
            groupService.updateSelective(group);
            //todo  删除用户与组的关联
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个用户组");
    }



}
